# -*- sh -*-
transport_src_list=""
transport_hdr_list=""
transport_def_list=""
transport_result_list=""
transport_add_list=""
new_with_transport_list=`echo $with_transports | $SED 's/,/ /g;'`
new_with_out_transport_list=`echo $with_out_transports | $SED 's/,/ /g;'`

# set up the CPP command
MODULECPP="$CPP $CPPFLAGS $PARTIALTARGETFLAGS -I${srcdir} -I${srcdir}/include"

#
# Add the default transports to the list
#
if test "x$PARTIALTARGETOS" = "xmingw32" -o "x$PARTIALTARGETOS" = "xmingw32msvc"; then 
  transport_default_list="UDP TCP Alias Callback"
else
  transport_default_list="UDP TCP Alias Unix Callback"
fi
if test "x$enable_ipv6" = "xyes"; then
  transport_default_list="UDPIPv6 TCPIPv6 $transport_default_list"
fi
for i in $transport_default_list; do
  if echo " $new_with_transport_list " | $GREP " $i " >/dev/null; then
    :
  else
    transport_add_list="$i $transport_add_list"
  fi
done
new_transport_list="$transport_add_list $new_with_transport_list"

if test "x$ctor_header" = "x" ; then
   ctor_header="snmplib/transports/snmp_transport_inits.h"
fi

echo "/* This file is automatically generated by configure.  Do not modify by hand. */" > $ctor_header
while test "x$new_transport_list" != "x"; do
  future_new_transport_list=""
  for i in $new_transport_list; do
    if echo " $new_with_out_transport_list " | $GREP " $i " >/dev/null; then
      true
    else
      if test "x$enable_ipv6" != "xyes"; then
          if test "x$i" = "xUDPIPv6" -o "x$i" = "xTCPIPv6"; then
              AC_MSG_ERROR(IPv6 transports not available if IPv6 support is not enabled)
          fi
      fi
      transport_src="transports/snmp"$i"Domain.c"
      transport_hdr="snmp"$i"Domain.h"
      rel_transport_src="snmplib/"$transport_src
      case $i in
	  IPBase)
	      rel_transport_hdr="snmplib/transports/$transport_hdr";;
	  *)
	      rel_transport_hdr="include/net-snmp/library/$transport_hdr";;
      esac
      lc_i=`echo $i | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 'abcdefghijklmnopqrstuvwxyz'`
      ctor_fn="netsnmp_"$lc_i"_ctor"
      if test -f "$srcdir/$rel_transport_src"; then
        transport_result_list="$transport_result_list $i"
        transport_src_list="$transport_src $transport_src_list"
        if test -f "$srcdir/$rel_transport_hdr"; then
          transport_hdr_list="$transport_hdr $transport_hdr_list"
        fi
        transport_def=`echo $i | $SED 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'`
        transport_def_list="NETSNMP_TRANSPORT_"$transport_def"_DOMAIN $transport_def_list"
      else
        echo
        AC_MSG_ERROR(Cannot find file $srcdir/$rel_transport_src to support SNMP transport domain $i.)
      fi

      #---------------------
      # build a pseudo include header that includes current config and
      # system symbols:
      rm -f conftest.$ac_ext module_tmp_header.i
      AC_LANG_CONFTEST([AC_LANG_SOURCE([[
#include NETSNMP_SYSTEM_INCLUDE_FILE
#undef NETSNMP_MINIMAL_CODE
#define NET_SNMP_CONFIG_H
#undef config_require
#define config_require(foo) config_checkit_require(foo)
#undef config_error
#define config_error(foo) config_checkit_error(foo)
#include "$rel_transport_hdr"]])])
      $MODULECPP conftest.$ac_ext >module_tmp_header.i 2>/dev/null

      #
      # check for dependencies using config_require() macro
      #
      sub_list=`cat module_tmp_header.i | $SED -n 's/.*config_checkit_require *( *\(.*\) *).*/\1/p'`
      if test "x$sub_list" != "x" ; then
        for j in $sub_list ; do
	  if echo " $future_new_transport_list $new_transport_list " | $GREP " $j " > /dev/null ; then
	    :
	  else
            future_new_transport_list="$future_new_transport_list $j"
	  fi
        done
      fi

      #
      # check for errors using config_error() macro
      #
      error_list=`cat module_tmp_header.i | $SED -n 's/.*config_checkit_error(\(.*\)).*/\1/gp'`
      if test "x$error_list" != "x" ; then
        echo ""
        AC_MSG_ERROR($error_list)
      fi

      #
      # Check for needed constructor initialization routines to call
      #
      found=`grep $ctor_fn $srcdir/$rel_transport_src`
      if grep $ctor_fn $srcdir/$rel_transport_src > /dev/null ; then
	# we add them down below instead of here to make sure
	# config_require dependency constructors get called first.
	tr_init_list="$ctor_fn $tr_init_list"
      fi
    fi
  done # for each new transport
  new_transport_list="$future_new_transport_list"
done # while new transports exist
transport_obj_list=`echo " $transport_src_list " | $SED 's/\.c/\.o/g'`
transport_lobj_list=`echo " $transport_src_list " | $SED 's/\.c/\.lo/g'`
transport_ftobj_list=`echo " $transport_src_list " | $SED 's/\.c/\.ft/g'`
for i in $tr_init_list ; do
   echo "$i();" >> $ctor_header
done

## win32 platforms require inet_ntop etc.
if test "x$PARTIALTARGETOS" = "xmingw32" -o "x$PARTIALTARGETOS" = "xmingw32msvc" -o "x$PARTIALTARGETOS" = "xcygwin"; then
  if echo "$transport_def_list" | $GREP "IPV6" >/dev/null; then
    transport_obj_list="$transport_obj_list inet_ntop.o inet_pton.o"
    transport_lobj_list="$transport_lobj_list inet_ntop.lo inet_pton.lo"
    transport_ftobj_list="$transport_ftobj_list inet_ntop.ft inet_pton.ft"
  fi
fi

if test "x$transport_result_list" = x ; then
   AC_MSG_ERROR([No transports configured, at least one must be provided])
fi
